<?php

/* 
 * Project: colobok
 * and open the template in the editor.
 * @author: Sviatoslav Mysachenko <svyatoslavik@gmail.com>
 */
use map\Bounds as Bounds;
use map\Tile;
class MapController extends CController{

   
    private function displayMapDataByBounds(Bounds $bounds, $zoom){
        $res = Item::model()->loadGroupedMarkers($bounds, $zoom);
        $responce = array();
        foreach($res as $val)
            $responce[$val->id] = array($val->lng, $val->lat, $val->attributes['counts']);
        echo json_encode($responce);
    }
    public function actionTile(){
        $tilecode = $_GET['code'];
        if(!$tilecode)return;
        $tile = \map\Tile::getTileByQuadTree($tilecode);
        $this->displayMapDataByBounds($tile->getBounds(), $tile->zoom());
    }
    public function actionSavemodel(){        
        if(!$_POST['data']) return;
        $data = json_decode($_POST['data'], true);
        if(!$data) return;
               
        $modelInstance = new Item();
        $modelInstance->lat = $data['lat'];
        $modelInstance->lng = $data['lng'];
        $modelInstance->save();
        echo json_encode(array('_id'=>(string)$modelInstance->id));
        
    }
}

